﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CreateStaticHtml.Pages
{
    public partial class NodeAdd : System.Web.UI.Page
    {
        public bool IsLogin = false;
        Interface.IProduct iProduct = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterHyperLink.NavigateUrl = "ProductNodes.aspx?docid=" + Request.QueryString["docid"];
            if (Session["user"] != null)
            {
                IsLogin = true;
            }
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            DavidDB.ADO.DBManager db = new DavidDB.ADO.DBManager();
            Interface.ITreeNode iTreeNode = Impl.ImplTreeNode.GetInstance(db);
            Models.TreeNode node = new Models.TreeNode();
            node.NodeName = this.UserName.Text;
            node.ProductId = Convert.ToInt32(Request.QueryString["docid"]);
            node.TreeNodeId = Convert.ToInt32(Request["parentid"]);

            iProduct = Impl.ImplProduct.GetInstance(db);
            Models.Product product = iProduct.GetModel(node.ProductId);
            node.Url = product.ProductName + "/";
            node.Url += GetURL(node.TreeNodeId, iTreeNode);

            node.Url += Request["ename"];

            int nodeid = iTreeNode.Insert(node);

            int allcount = Convert.ToInt32(Request["scount"]);

            Interface.ISection iSection = Impl.ImplSection.GetInstance(db);

            for (int i = 0; i < allcount; i++)
            {
                if (!string.IsNullOrEmpty(Request["section" + (i + 1)]))
                {
                    Models.Section section = new Models.Section();
                    section.Content = Request["section" + (i + 1)];
                    section.Prower = i;
                    section.TreeNodeId = nodeid;
                    db.Insert(section);
                }
            }

            db.Commit();
            db.Dispose();
        }
        private string baseURL = "";
        public string GetURL(int nodeId, Interface.ITreeNode iTreeNode)
        {
            if (nodeId != 0)
            {
                Models.TreeNode parentNode = iTreeNode.GetModel(nodeId);
                baseURL = parentNode.EName + "/" + baseURL + "/";
                return GetURL(parentNode.TreeNodeId, iTreeNode);
            }
            else
            {
                return baseURL;
            }
        }
    }
}